package cn.iocoder.yudao.module.product.property.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.property.dal.dataobject.ProductPropertyDO;
import cn.iocoder.yudao.module.product.property.service.ProductPropertyService;
import cn.iocoder.yudao.module.product.property.vo.AppProductPropertyRespVO;
import cn.iocoder.yudao.module.product.property.vo.AppProductPropertySaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 产品 SKU项")
@RestController
@RequestMapping("/product/property")
@Validated
public class AppProductPropertyController {

    @Resource
    private ProductPropertyService productPropertyService;

    @PostMapping("/create")
    @Operation(summary = "创建规格项")
    public CommonResult<Long> createProperty(@Valid @RequestBody AppProductPropertySaveReqVO createReqVO) {
        return success(productPropertyService.createProperty(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新规格项")
    public CommonResult<Boolean> updateProperty(@Valid @RequestBody AppProductPropertySaveReqVO updateReqVO) {
        productPropertyService.updateProperty(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除规格项")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProperty(@RequestParam("id") Long id) {
        productPropertyService.deletePropertyById(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规格项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppProductPropertyRespVO> getProperty(@RequestParam("id") Long id) {
        ProductPropertyDO property = productPropertyService.getProperty(id);
        return success(BeanUtils.toBean(property, AppProductPropertyRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得规格项列表")
    public CommonResult<List<AppProductPropertyRespVO>> getPropertyList() {
        List<ProductPropertyDO> list = productPropertyService.getPropertyList();
        return success(BeanUtils.toBean(list, AppProductPropertyRespVO.class));
    }

}
